from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from WebTest.WebAutomationEncapsulation.PublicToolsCategory.My_mysql import My_mysql
import time


class UserRegisterPage:
	def __init__(self, driver):
		self.driver = driver
		
	def register_page(self, username, password, repassword):
		#判断是否进入登录页面
		WebDriverWait(self.driver, 10, 0.5).until(EC.title_is("度小满理财-登录[内测版]"))
		#点击注册按钮
		self.driver.find_element(By.PARTIAL_LINK_TEXT, '去注册').click()
		WebDriverWait(self.driver, 10, 0.5).until(EC.title_is("个人理财系统注册界面"))
		time.sleep(1)
		self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys(username)
		self.driver.find_element(By.CSS_SELECTOR, '#password').send_keys(password)
		self.driver.find_element(By.CSS_SELECTOR, '#repassword').send_keys(repassword)
		self.driver.find_element(By.TAG_NAME, "button").click()
	
	def register_success_get_massage(self):
		return self.driver.title
	
	def register_massage_sql_select(self, username):
		db = My_mysql()
		db.get_mysql(host="localhost", user="root", password="root", database="finance",port=3306)
		sql = "select * from user where username = %s"
		params = [username]
		return db.select_sql(sql, params)

	def register_massage_sql_delete(self, username):
		db = My_mysql()
		db.get_mysql(host="localhost", user="root", password="root", database="finance",port=3306)
		sql = "delete from user where username = %s"
		params = [username]
		return db.update_sql(sql, params)
	
	def register_username_error_get_massage(self):
		return self.driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[1]/span[1]').text
	
	def register_password_error_get_massage(self):
		# 获取密码错误提示信息
		return self.driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[2]/span[1]').text
	
	def register_repassword_error_get_massage(self):
		# 获取确认密码错误提示信息
		return self.driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[3]/span[1]').text
	
	